package com.bona.dubbo.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.bona.dubbo.system.ISysUserDubboSV;
import com.bona.model.system.SysFunctionModel;
import com.bona.model.system.SysIconModel;
import com.bona.model.system.SysUserModel;
import com.bona.service.system.SysCommonService;
import com.bona.service.system.SysFunctionService;
import com.bona.service.system.SysIconService;
import com.bona.service.system.SysOrgService;
import com.bona.service.system.SysRoleService;
import com.bona.service.system.SysUserService;
import com.bona.util.common.StringUtil;

public class SysUserDubboSVImpl implements ISysUserDubboSV {

	@Autowired
	private SysCommonService commonService;
	@Autowired
	private SysUserService userService;
	@Autowired
	private SysOrgService orgService;
	@Autowired
	private SysRoleService roleService;
	@Autowired
	private SysFunctionService functionService;
	@Autowired
	private SysIconService iconService;
	
	@Override
	public SysUserModel query(SysUserModel model) throws Exception {
		return userService.query(model);
	}

	@Override
	public SysUserModel queryLogin(String username, String password) {
		
		SysUserModel model = new SysUserModel();
		model.setUsername(username);
		model.setPassword(password);
		model = userService.query(model);
		return model;
	}

	@Override
	public List<SysFunctionModel> queryFunctionByUserId(String userId) {
		
		List<String> roleIds = userService.queryRoleIdsByUserId(userId);
		List<SysFunctionModel> functionModels = functionService.queryFunctionByRoleIds(roleIds);
		for (SysFunctionModel functionModel: functionModels) {
			if (StringUtil.isNotEmpty(functionModel.getIconid())) {
				SysIconModel sysIcon = iconService.query(new SysIconModel(functionModel.getIconid()));
				functionModel.setSysIcon(sysIcon);
			}
			if (StringUtil.isNotEmpty(functionModel.getDeskIconid())) {
				SysIconModel sysDeskIcon = iconService.query(new SysIconModel(functionModel.getDeskIconid()));
				functionModel.setDeskSysIcon(sysDeskIcon);
			}
			if (functionModel.getFunctionlevel() > 0) {
				SysFunctionModel fmodel = new SysFunctionModel();
				fmodel.setParentfunctionid(functionModel.getId());
				List<SysFunctionModel> childFunction = functionService.queryByPage(fmodel);
				if (childFunction!=null && childFunction.size()>0) {
					functionModel.setChildFunction(childFunction);
				}
			}
		}
		return functionModels;
	}

	@Override
	public Integer updateUser(SysUserModel model) throws Exception {
		
		return userService.update(model);
	}

	@Override
	public SysUserModel queryUserByName(String username) {
		
		SysUserModel model = new SysUserModel();
		model.setUsername(username);
		model = userService.query(model);
		return model;
	}

	@Override
	public void add(SysUserModel user, String roleIds, String orgIds) {
		userService.add(user, roleIds, orgIds);
	}
	
	@Override
	public Integer queryByCount(SysUserModel model) {
		
		return userService.queryByCount(model);
	}

	@Override
	public List<SysUserModel> queryByPage(SysUserModel model) {
		
		return userService.queryByPage(model);
	}

	@Override
	public void deleteUser(String userId) {
		userService.deleteUser(userId);
	}

	@Override
	public List<String> queryOrgIdsByUserId(String userId) {
		return userService.queryOrgIdsByUserId(userId);
	}

}
